home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Reaction / Integer / Integer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-02  |  6.5 KB  |  284 lines

  1. /**
  2. *
  3. * COPYRIGHT:
  4. *
  5. *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  6. *   All rights reserved.
  7. *
  8. * DISCLAIMER:
  9. *
  10. *   This software is provided "as is". No representations or warranties
  11. *   are made with respect to the accuracy, reliability, performance,
  12. *   currentness, or operation of this software, and all use is at your
  13. *   own risk. Neither Amiga nor the authors assume any responsibility
  14. *   or liability whatsoever with respect to your use of this software.
  15. *
  16.  
  17.  **  IntegerExample.c -- Integer class Example.
  18.  **
  19.  **  This is a simple example testing some of the capabilities of the
  20.  **  Integer gadget class.
  21.  **
  22.  **  This code opens a window and then creates 2 Integer gadgets which
  23.  **  are subsequently attached to the window's gadget list.  One uses
  24.  **  arrows, one does not.  Notice that you can tab cycle between them.
  25.  **/
  26.  
  27. /* system includes
  28.  */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. #include <exec/types.h>
  34. #include <exec/memory.h>
  35. #include <intuition/intuition.h>
  36. #include <intuition/gadgetclass.h>
  37. #include <intuition/classes.h>
  38. #include <graphics/gfxbase.h>
  39. #include <graphics/text.h>
  40. #include <graphics/gfxmacros.h>
  41. #include <utility/tagitem.h>
  42. #include <workbench/startup.h>
  43. #include <workbench/workbench.h>
  44.  
  45. #include <clib/reaction_lib_protos.h>
  46. #include <clib/alib_protos.h>
  47. #include <proto/intuition.h>
  48. #include <proto/exec.h>
  49. #include <proto/layout.h>
  50. #include <proto/integer.h>
  51. #include <proto/button.h>
  52. #include <proto/window.h>
  53. #include <proto/label.h>
  54.  
  55. #include <gadgets/integer.h>
  56. #include <gadgets/layout.h>
  57. #include <gadgets/button.h>
  58. #include <classes/window.h>
  59. #include <images/label.h>
  60.  
  61. #include <reaction/reaction.h>
  62. #include <reaction/reaction_macros.h>
  63.  
  64. enum
  65. {
  66.     GID_MAIN=0,
  67.     GID_INTEGER1,
  68.     GID_INTEGER2,
  69.     GID_DOWN,
  70.     GID_UP,
  71.     GID_QUIT,
  72.     GID_LAST
  73. };
  74.  
  75. enum
  76. {
  77.     WID_MAIN=0,
  78.     WID_LAST
  79. };
  80.  
  81. enum
  82. {
  83.     OID_MAIN=0,
  84.     OID_LAST
  85. };
  86.  
  87.  
  88. struct Library *WindowBase = NULL;
  89. struct Library *LayoutBase = NULL;
  90. struct Library *IntegerBase = NULL;
  91. struct Library *ButtonBase = NULL;
  92. struct Library *LabelBase = NULL;
  93.  
  94. void openlibs(void) 
  95. {
  96.     /* Open the classes - typically not required to be done manually.
  97.      * SAS/C or DICE AutoInit can do this for you if linked with the
  98.      * supplied reaction.lib
  99.      */
  100.     
  101.     WindowBase = (struct Library *)OpenLibrary("window.class",44);
  102.     LayoutBase = (struct Library *)OpenLibrary("gadgets/layout.gadget",44);
  103.     ButtonBase = (struct Library *)OpenLibrary("gadgets/button.gadget",44);
  104.     IntegerBase = (struct Library *)OpenLibrary("gadgets/integer.gadget",44);
  105.     LabelBase = (struct Library *)OpenLibrary("images/label.image",44);
  106.  
  107. }
  108.  
  109. void closelibs(void) 
  110. {
  111.     /* Close the classes.
  112.      */
  113.     CloseLibrary( LabelBase);
  114.     CloseLibrary( IntegerBase);
  115.     CloseLibrary( ButtonBase );
  116.     CloseLibrary( LayoutBase );
  117.     CloseLibrary( WindowBase );
  118. }
  119.  
  120. int main(void)
  121. {
  122.     struct MsgPort *AppPort;
  123.  
  124.     struct Window *windows[WID_LAST];
  125.  
  126.     struct Gadget *gadgets[GID_LAST];
  127.  
  128.     Object *objects[OID_LAST];
  129.  
  130.     /* make sure our classes opened... */
  131.     openlibs();
  132.     if (!ButtonBase || !IntegerBase || !WindowBase || !LayoutBase || !LabelBase) {
  133.          printf("A Library could not be opened\n");
  134.         return(30);
  135.     }    
  136.     else if ( AppPort = CreateMsgPort() )
  137.     {
  138.         /* Create the window object.
  139.          */
  140.         objects[OID_MAIN] = WindowObject,
  141.             WA_ScreenTitle, "ReAction",
  142.             WA_Title, "ReAction Integer Example",
  143.             WA_Activate, TRUE,
  144.             WA_DepthGadget, TRUE,
  145.             WA_DragBar, TRUE,
  146.             WA_CloseGadget, TRUE,
  147.             WA_SizeGadget, TRUE,
  148.             WINDOW_IconifyGadget, TRUE,
  149.             WINDOW_IconTitle, "Integer",
  150.             WINDOW_AppPort, AppPort,
  151.             WINDOW_Position, WPOS_CENTERMOUSE,
  152.             WINDOW_ParentGroup, gadgets[GID_MAIN] = VGroupObject,
  153.                 LAYOUT_SpaceOuter, TRUE,
  154.                 LAYOUT_DeferLayout, TRUE,
  155.  
  156.                 LAYOUT_AddChild, gadgets[GID_INTEGER1] = IntegerObject,
  157.                     GA_ID, GID_INTEGER1,
  158.                     GA_RelVerify, TRUE,
  159.                     GA_TabCycle, TRUE,
  160.                     INTEGER_Arrows, TRUE,
  161.                     INTEGER_MaxChars, 3,
  162.                     INTEGER_Minimum, -32,
  163.                     INTEGER_Maximum, 32,
  164.                     INTEGER_Number, 0,
  165.                 IntegerEnd,
  166.                 CHILD_NominalSize, TRUE,
  167.                 CHILD_Label, LabelObject, LABEL_Text, "Integer _1", LabelEnd,
  168.  
  169.                 LAYOUT_AddChild, gadgets[GID_INTEGER2] = IntegerObject,
  170.                     GA_ID, GID_INTEGER2,
  171.                     GA_RelVerify, TRUE,
  172.                     GA_TabCycle, TRUE,
  173.                     INTEGER_Arrows, FALSE,
  174.                     INTEGER_MaxChars, 6,
  175.                     INTEGER_Minimum, 0,
  176.                     INTEGER_Maximum, 100000,
  177.                     INTEGER_Number, 100,
  178.                 IntegerEnd,
  179.                 CHILD_Label, LabelObject, LABEL_Text, "Integer _2", LabelEnd,
  180.  
  181.                 LAYOUT_AddChild, ButtonObject,
  182.                     GA_ID, GID_QUIT,
  183.                     GA_RelVerify, TRUE,
  184.                     GA_Text,"_Quit",
  185.                 ButtonEnd,
  186.                 CHILD_WeightedHeight, 0,
  187.  
  188.             EndGroup,
  189.         EndWindow;
  190.  
  191.          /*  Object creation sucessful?
  192.           */
  193.         if (objects[OID_MAIN])
  194.         {
  195.             /*  Open the window.
  196.              */
  197.             if (windows[WID_MAIN] = (struct Window *) RA_OpenWindow(objects[OID_MAIN]))
  198.             {
  199.                 ULONG wait, signal, app = (1L << AppPort->mp_SigBit);
  200.                 ULONG done = FALSE;
  201.                 ULONG result;
  202.                 UWORD code;
  203.  
  204.                  /* Obtain the window wait signal mask.
  205.                  */
  206.                 GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  207.  
  208.                 /* Activate the first integer gadget!
  209.                  */
  210.                 ActivateLayoutGadget( gadgets[GID_MAIN], windows[WID_MAIN], NULL, (Object) gadgets[GID_INTEGER1] );
  211.  
  212.                 /* Input Event Loop
  213.                  */
  214.                 while (!done)
  215.                 {
  216.                     wait = Wait( signal | SIGBREAKF_CTRL_C | app );
  217.  
  218.                     if ( wait & SIGBREAKF_CTRL_C )
  219.                     {
  220.                         done = TRUE;
  221.                     }
  222.                     else
  223.                     {
  224.                         while ( (result = RA_HandleInput(objects[OID_MAIN], &code) ) != WMHI_LASTMSG )
  225.                         {
  226.                             switch (result & WMHI_CLASSMASK)
  227.                             {
  228.                                 case WMHI_CLOSEWINDOW:
  229.                                     windows[WID_MAIN] = NULL;
  230.                                     done = TRUE;
  231.                                     break;
  232.  
  233.                                 case WMHI_GADGETUP:
  234.                                     switch (result & WMHI_GADGETMASK)
  235.                                     {
  236.                                         case GID_QUIT:
  237.                                             done = TRUE;
  238.                                             break;
  239.                                     }
  240.                                     break;
  241.  
  242.                                 case WMHI_ICONIFY:
  243.                                     RA_Iconify(objects[OID_MAIN]);
  244.                                     windows[WID_MAIN] = NULL;
  245.                                     break;
  246.  
  247.                                 case WMHI_UNICONIFY:
  248.                                     windows[WID_MAIN] = (struct Window *) RA_OpenWindow(objects[OID_MAIN]);
  249.  
  250.                                     if (windows[WID_MAIN])
  251.                                     {
  252.                                         GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  253.                                     }
  254.                                     else
  255.                                     {
  256.                                         done = TRUE;    // error re-opening window!
  257.                                     }
  258.                                      break;
  259.                             }
  260.                         }
  261.                     }
  262.                 }
  263.             }
  264.  
  265.             /* Disposing of the window object will also close the window if it is
  266.              * already opened, and it will dispose of the layout object attached to it.
  267.              */
  268.             DisposeObject(objects[OID_MAIN]);
  269.         }
  270.  
  271.         DeleteMsgPort(AppPort);
  272.     }
  273.  
  274.     closelibs();
  275.     return(0);
  276. }
  277.  
  278. #ifdef __STORM__
  279. int wbmain(struct WBStartup *wb) 
  280. {
  281.     return(main() );    
  282. }
  283. #endif
  284.